#include "yuv.h"
#include <android/log.h>

#define LOG_TAG_YUV "yuv"

//#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG_YUV,__VA_ARGS__)

const unsigned char table[] = {

/*--  文字:  陈  --*/
/*--  楷体_GB231212;  此字体下对应的点阵为：宽x高=16x16   --*/
0x00, 0x40, 0x78, 0x40, 0x48, 0x40, 0x57, 0xFE, 0x50, 0x80, 0x61, 0x20, 0x51,
		0x20, 0x4A, 0x20, 0x4B, 0xFC, 0x48, 0x20, 0x69, 0x28, 0x51, 0x24, 0x42,
		0x22, 0x44, 0x22, 0x40, 0xA0, 0x40, 0x40,

		/*--  文字:  桂  --*/
		/*--  楷体_GB231212;  此字体下对应的点阵为：宽x高=16x16   --*/
		0x10, 0x20, 0x10, 0x20, 0x11, 0xFC, 0x10, 0x20, 0xFC, 0x20, 0x10, 0x20,
		0x33, 0xFE, 0x38, 0x00, 0x54, 0x20, 0x54, 0x20, 0x91, 0xFC, 0x10, 0x20,
		0x10, 0x20, 0x10, 0x20, 0x13, 0xFE, 0x10, 0x00,

		/*--  文字:  芳  --*/
		/*--  楷体_GB231212;  此字体下对应的点阵为：宽x高=16x16   --*/
		0x08, 0x20, 0x08, 0x20, 0xFF, 0xFE, 0x08, 0x20, 0x0A, 0x20, 0x01, 0x00,
		0xFF, 0xFE, 0x04, 0x00, 0x04, 0x00, 0x07, 0xF0, 0x04, 0x10, 0x08, 0x10,
		0x08, 0x10, 0x10, 0x10, 0x20, 0xA0, 0x40, 0x40,

};

const unsigned char byteTable[] = {
/*--  文字:  趣  --*//*--  楷体_GB231212;  此字体下对应的点阵为：宽x高=16x16   --*/
0x10, 0x00, 0x13, 0xE0, 0x11, 0x5E, 0x7D, 0x42, 0x11, 0xD2, 0x11, 0x4A, 0xFD,
		0x4A, 0x11, 0xC4, 0x11, 0x44, 0x51, 0x64, 0x5D, 0xCA, 0x53, 0x4A, 0x70,
		0x52, 0x50, 0x40, 0x4F, 0xFE, 0x80, 0x00,
		/*--  文字:  看  --*/
		0x00, 0xF8, 0x7F, 0x00, 0x01, 0x00, 0x3F, 0xF8, 0x02, 0x00, 0xFF, 0xFE,
		0x04, 0x00, 0x08, 0x00, 0x1F, 0xF0, 0x28, 0x10, 0x4F, 0xF0, 0x88, 0x10,
		0x0F, 0xF0, 0x08, 0x10, 0x0F, 0xF0, 0x08, 0x10 };
const unsigned char charTable[] = {

/*--  文字:  0 --*/
0x00, 0x00, 0x00, 0x18, 0x24, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x24,
		0x18, 0x00, 0x00,

		/*--  文字:  1 --*/
		0x00, 0x00, 0x00, 0x10, 0x70, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
		0x10, 0x7C, 0x00, 0x00,

		/*--  文字:  2  --*/
		0x00, 0x00, 0x00, 0x3C, 0x42, 0x42, 0x42, 0x04, 0x04, 0x08, 0x10, 0x20,
		0x42, 0x7E, 0x00, 0x00,

		/*--  文字:  3  --*/
		0x00, 0x00, 0x00, 0x3C, 0x42, 0x42, 0x04, 0x18, 0x04, 0x02, 0x02, 0x42,
		0x44, 0x38, 0x00, 0x00,

		/*--  文字:  4  --*/
		0x00, 0x00, 0x00, 0x04, 0x0C, 0x14, 0x24, 0x24, 0x44, 0x44, 0x7E, 0x04,
		0x04, 0x1E, 0x00, 0x00,

		/*--  文字:  5  --*/
		0x00, 0x00, 0x00, 0x7E, 0x40, 0x40, 0x40, 0x58, 0x64, 0x02, 0x02, 0x42,
		0x44, 0x38, 0x00, 0x00,

		/*--  文字:  6  --*/
		0x00, 0x00, 0x00, 0x1C, 0x24, 0x40, 0x40, 0x58, 0x64, 0x42, 0x42, 0x42,
		0x24, 0x18, 0x00, 0x00,

		/*--  文字:  7  --*/
		0x00, 0x00, 0x00, 0x7E, 0x44, 0x44, 0x08, 0x08, 0x10, 0x10, 0x10, 0x10,
		0x10, 0x10, 0x00, 0x00,

		/*--  文字:  8  --*/
		0x00, 0x00, 0x00, 0x3C, 0x42, 0x42, 0x42, 0x24, 0x18, 0x24, 0x42, 0x42,
		0x42, 0x3C, 0x00, 0x00,

		/*--  文字:  9  --*/
		0x00, 0x00, 0x00, 0x18, 0x24, 0x42, 0x42, 0x42, 0x26, 0x1A, 0x02, 0x02,
		0x24, 0x38, 0x00, 0x00,

		/*--  文字:  ":"  --*/
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00,
		0x18, 0x18, 0x00, 0x00,

		/*--  文字:  "-"  --*/
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00 };

/*
 * Function:     draw_Font_Func
 * Description:  实现在yuv420图片上面画字
 * Input:        char *ptr_frame             一帧视频的首地址
 *               const unsigned char font[]  画的字模
 *               int startx                  写字的起点坐标x
 *               int starty                  写字的起点坐标y
 *               int color                   字颜色的选择，具体颜色在程序代码
 * Return:       这里会把传进来的一帧视频的地址返回，可以不调用
 */
char *draw_Font_Func(char *ptr_frame, int FRAME_WIDTH, int FRAME_HEIGHT,
		int startx, int starty, int color, char *str, char * nameTable,
		int count) {

	assert(ptr_frame != NULL);

	int tagY = 0, tagU = 0, tagV = 0;
	char *offsetY = NULL, *offsetU = NULL, *offsetV = NULL;
	unsigned short p16, mask16; // for reading hzk16 dots

	unsigned char p8, mask8;

	unsigned int nStrlen = strlen(str);
	/***
	 *  i 表示汉字的个数
	 *  j 表示汉字的16x16点阵序列值
	 *  x 表示yuv绘制汉字的X轴偏移量
	 *  y 表示yuv绘制汉字的Y轴偏移量
	 * */
	int x = 0, y = 0, i = 1, j = 0, k = 0;

	/*yuv 地址的设置 */
	offsetY = ptr_frame;											// Y分量起始地址
	offsetU = offsetY + FRAME_WIDTH * FRAME_HEIGHT;		// U分量起始地址
	offsetV = offsetU + FRAME_WIDTH * FRAME_HEIGHT / 4; // V分量起始地址

	// 添加汉字
	if (nameTable != NULL && count > 0) {
		for (i = 0; i < count; i++) {
			for (j = 0, y = starty; j < 16 && y < FRAME_HEIGHT - 1; j++, y++) {
				p16 = *(unsigned short *) (nameTable + j * 2 + i * 32);/*取字模数据*/
				mask16 = 0x0080; /* 二进制 1000 0000 */
				for (k = 0, x = startx + i * 18; k < 16 && x < FRAME_WIDTH - 1;
						k++, x++)    // dots in a line
						{
					if (p16 & mask16) {
						if (FRAME_WIDTH > 640 || FRAME_HEIGHT > 640) {
							*(offsetY + 2 * y * FRAME_WIDTH + 2*x) = 255;
							*(offsetY + (2 * y + 1) * FRAME_WIDTH + 2*x) = 255;
						} else {
							*(offsetY + y * FRAME_WIDTH + x) = 255;
						}
					}
					mask16 = mask16 >> 1; /* 循环移位取数据 */
					if (mask16 == 0)
						mask16 = 0x8000;
				}
			}
		}
		startx += (count+1) * 18;
	}

	// 添加时间字符串
	for (i = 0; i < nStrlen; i++) {
		int temp1 = str[i] - 48;
		if (temp1 == -3) {
			//"-"
			temp1 = 11;
		} else if (temp1 > 9) {
			temp1 = 10;
		} else if (temp1 < 0) {
			continue;
		}

		//j是点阵的高度，k点阵的宽度
		// 循环line dots per char
		for (j = 0, y = starty; j < 16 && y < FRAME_HEIGHT - 1; j++, y++) {
			p8 = *(char *) (charTable + j + temp1 * 16);/*取字模数据*/
			mask8 = 0x80;
			for (k = 0, x = startx + i * 8; k < 8 && x < FRAME_WIDTH - 1;
					k++, x++)    // dots in a line
					{
				if (p8 & mask8) {
					if (FRAME_WIDTH > 640 || FRAME_HEIGHT > 640) {
						*(offsetY + 2 * y * FRAME_WIDTH + 2*x) =255;
						*(offsetY + (2 * y + 1) * FRAME_WIDTH + 2*x)= 255;
					} else {
						*(offsetY + y * FRAME_WIDTH + x) = 255;
					}
				}
				mask8 = mask8 >> 1; /* 循环移位取数据 */
				if (mask8 == 0)
					mask8 = 0x80;
			}
		}
	}

	return (char *) ptr_frame;
}
